package com.light.buttons;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;

import com.light.gamecomponents.GameForm;
import com.light.roller.Direction;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class ExitButton extends Button
{
	private float m_onScreenPositionX;
	private float m_onScreenPositionY;
	private Direction m_initialDirection;
    public float getOnScreenPositionX() { return m_onScreenPositionX; }
    public float getOnScreenPositionY() { return m_onScreenPositionY; }
    
	protected float[] m_positionXs = { Roller.CAMERA_WIDTH * 0.92f,
            						   Roller.CAMERA_WIDTH * 0.05f,
            						   Roller.CAMERA_WIDTH * 0.95f,
            						   Roller.CAMERA_WIDTH * 0.08f };

	protected float[] m_positionYs = { Roller.CAMERA_HEIGHT * 0.1f,
									   Roller.CAMERA_HEIGHT * 0.1f,
									   Roller.CAMERA_HEIGHT * 0.9f,
									   Roller.CAMERA_HEIGHT * 0.9f };
	
	private GameForm m_form;

	protected AnimatedSprite m_animatedSprite;
	protected TiledTextureRegion m_tiledTextureRegion;
	
	public ExitButton(Roller game, GameForm form,Direction direction) {
		super(game);
		m_state = Button.ButtonState.NOT_CONTROLABLE;
		m_form = form;
		m_initialDirection = direction;
	}	
	
	
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (m_animatedSprite != null)
		{
	    	float[] accelerometer_values = event.values.clone();
	    	if (Roller.orientation == Configuration.ORIENTATION_PORTRAIT)
	    	{
		    	if (Math.abs(accelerometer_values[1]) >= Math.abs(accelerometer_values[0]))
		    	{
		    		if (accelerometer_values[1] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.LEFT)
		    		{
		    			int centerX = (int)(m_positionXs[2] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[2] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.LEFT;
		    			m_animatedSprite.setRotation(90);
		    		}
		    		else if (accelerometer_values[1] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.RIGHT)
		    		{
		    			int centerX = (int)(m_positionXs[1] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[1] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.RIGHT;
		    			m_animatedSprite.setRotation(-90);
		    		}
		    	}
		    	else
		    	{
		    		if (accelerometer_values[0] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.DOWN)
		    		{
		    			int centerX = (int)(m_positionXs[3] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[3] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.DOWN;
		    			m_animatedSprite.setRotation(180);
		    		}
		    		else if (accelerometer_values[0] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.UP)
		    		{
		    			int centerX = (int)(m_positionXs[0] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[0] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.UP;
		    			m_animatedSprite.setRotation(0);
		    		}
		    	}
	    	}
	    	else
	    	{
		    	if (Math.abs(accelerometer_values[1]) >= Math.abs(accelerometer_values[0]))
		    	{
		    		if (accelerometer_values[1] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.DOWN)
		    		{
		    			int centerX = (int)(m_positionXs[3] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[3] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.DOWN;
		    			m_animatedSprite.setRotation(180);
		    		}
		    		else if (accelerometer_values[1] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.UP)
		    		{
		    			int centerX = (int)(m_positionXs[0] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[0] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.UP;
		    			m_animatedSprite.setRotation(0);
		    		}
		    	}
		    	else
		    	{
		    		if (accelerometer_values[0] <= -Roller.GSENSOR_SENSITIVITY && m_direction != Direction.RIGHT)
		    		{
		    			int centerX = (int)(m_positionXs[1] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[1] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.RIGHT;
		    			m_animatedSprite.setRotation(-90);
		    		}
		    		else if (accelerometer_values[0] > Roller.GSENSOR_SENSITIVITY && m_direction != Direction.LEFT)
		    		{
		    			int centerX = (int)(m_positionXs[2] - this.m_animatedSprite.getWidth() / 2);
		    	        int centerY =  (int)(m_positionYs[2] - this.m_animatedSprite.getHeight() / 2);
		    	        m_onScreenPositionX = centerX;
		    	        m_onScreenPositionY = centerY;
		    			m_direction = Direction.LEFT;
		    			m_animatedSprite.setRotation(90);
		    		}
		    	}
	    	}
		}
		
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		Camera tempCamera = Roller.cameraManager.getCamera();
    	setByCameraPosition(tempCamera.getCenterX(), tempCamera.getCenterY());
		
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources("ExitButton", m_game);
		this.m_tiledTextureRegion = ResourceManager.getTiledTextureRegion("ExitButton");
	}

	@Override
	public void unLoadResources() {
		this.m_tiledTextureRegion = null;
		ResourceManager.unLoadResources("ExitButton", m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) 
	{
		int centerX = (int)(m_positionXs[0] - this.m_tiledTextureRegion.getTileWidth() / 2);
        int centerY =  (int)(m_positionYs[0] - this.m_tiledTextureRegion.getTileHeight() / 2);
        m_onScreenPositionX = centerX;
        m_onScreenPositionY = centerY;
        m_animatedSprite = new AnimatedSprite(centerX, centerY, this.m_tiledTextureRegion)
		{
			@Override
        	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
        	{
				if (pSceneTouchEvent.isActionUp())
				{
					if (m_state == ButtonState.CONTROLABLE)
					{
						if (m_form.isHiding())
						{
							m_form.showForm();
							((Roller)m_game).greyButton();
							((Roller)m_game).pause();
							Roller.actionButton.setState(ButtonState.MENU_SHOWED);
							Roller.actionButton.changeToSwitchButton();
						}
						else
						{
							m_form.hideForm();
							((Roller)m_game).ungreyButton();
							((Roller)m_game).unpause();
						}
					}
					else
						changeToMenu();
				}
				return true;
        	}
		};
		
		if (Roller.PreNum==Roller.CurrentNum)
			m_animatedSprite.setCurrentTileIndex(0);
		else 
			m_animatedSprite.setCurrentTileIndex(1);
		
		scene.registerTouchArea(m_animatedSprite);
		scene.attachChild(m_animatedSprite);
		initializeDirection(m_initialDirection);
	}
	public void changeToMenu()
	{
		m_animatedSprite.setCurrentTileIndex(0);
		setState(Button.ButtonState.CONTROLABLE);
		Roller.stage.endPrepare();
		Roller.cameraManager.skipShowingDoor();
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_animatedSprite);
		scene.unregisterTouchArea(m_animatedSprite);
		m_animatedSprite = null;
	}

	@Override
	public void setByCameraPosition(float cameraX, float cameraY) {
		m_animatedSprite.setPosition(cameraX + m_onScreenPositionX - Roller.CAMERA_WIDTH / 2.0f,
				 			 		 cameraY + m_onScreenPositionY - Roller.CAMERA_HEIGHT / 2.0f);
	}
	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void initializeDirection(Direction direction) {
		int centerX;
        int centerY;
		switch (direction)
		{
		case UP:
			centerX = (int)(m_positionXs[0] - this.m_animatedSprite.getWidth() / 2);
	        centerY =  (int)(m_positionYs[0] - this.m_animatedSprite.getHeight() / 2);
	        m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.UP;
			m_animatedSprite.setRotation(0);
			break;
		case DOWN:
			centerX = (int)(m_positionXs[3] - this.m_animatedSprite.getWidth() / 2);
		    centerY =  (int)(m_positionYs[3] - this.m_animatedSprite.getHeight() / 2);
		    m_onScreenPositionX = centerX;
		    m_onScreenPositionY = centerY;
			m_direction = Direction.DOWN;
			m_animatedSprite.setRotation(180);
			break;
		case LEFT:
			centerX = (int)(m_positionXs[2] - this.m_animatedSprite.getWidth() / 2);
		    centerY =  (int)(m_positionYs[2] - this.m_animatedSprite.getHeight() / 2);
		    m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.LEFT;
			m_animatedSprite.setRotation(90);
			break;
		case RIGHT:
			centerX = (int)(m_positionXs[1] - this.m_animatedSprite.getWidth() / 2);
	        centerY =  (int)(m_positionYs[1] - this.m_animatedSprite.getHeight() / 2);
	        m_onScreenPositionX = centerX;
	        m_onScreenPositionY = centerY;
			m_direction = Direction.RIGHT;
			m_animatedSprite.setRotation(-90);
			break;
		}
	}
}
